<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model as ThinkModel;

//银行列表模型
class ViperActivityProject extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_VIPER_ACTIVITY_PROJECT__';
    protected $name = 'qt_viper_activity_project';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    public static function createByTemplate($arr,$store_id,$viper_type_id)
    {
        foreach ($arr as $item)
        {

            if(!isset($item['type']))$item['type'] = null;
            if(!isset($item['value']))$item['value'] = null;
            if(!isset($item['start']))$item['start'] = null;
            if(!isset($item['end']))$item['end'] = null;
            if(!isset($item['range_viper_price']))$item['range_viper_price'] = null;
            if(!isset($item['rule']))$item['rule'] = null;
            if(!isset($item['range_add_block_price']))$item['range_add_block_price'] = null;
            self::create([
                'project_id' => $item['id'],
                'type' => $item['type'],
                'value' =>$item['value'],
                'start' => $item['start'],
                'end' => $item['end'],
                'viper_price' => $item['viper_price'],
                'add_block_price' => $item['add_block_price'],
                'range_viper_price' => $item['range_viper_price'],
                'range_add_block_price' => $item['range_add_block_price'],
                'rule' => $item['rule'],
                'viper_type_id' => $viper_type_id,
                'store_id' => $store_id
            ]);
        }
    }

    public static function getActivityProjectOfStore($store_id,$viper_type_id)
    {
        $list = self::alias('a')
            ->leftJoin('dp_qt_project p','a.project_id=p.id')
            ->where(
                ['a.store_id'=>$store_id,'a.viper_type_id'=>$viper_type_id])
            ->field('a.project_id id,a.type,a.value,a.start,a.end,a.viper_price,a.add_block_price,a.range_add_block_price,a.range_viper_price,a.rule,p.name,a.id as ac_id')
            ->select();
        //删除无效的商品
        foreach ($list as $key=>$value){
            if(empty($value['name'])){
                self::where(['id'=>$value['ac_id']])->delete();
            }
        }

        return $list;
    }

}